/*
 * Copyright 2002-2006 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.tigris.jasmincc.implementation;

import org.tigris.jasmincc.Production;
import org.tigris.jasmincc.framework.*;

/**
 * <strong>Syntax</strong><br />
 * <code>[<strong>namespace</strong> org.jasmincc;]<br />
 *   <br />
 *   (<strong>include</strong> org.javacc;)*<br />
 *   |<br />
 *   (<strong>import</strong> org.javacc;)*<br />
 *   <br />
 *   ( grammarDeclaration() )*<br />
 *   |<br />
 *   grammarBodyDeclaration()<br />
 *	</code>
 * 
 * @author Sam Huang
 * 
 */
public class GrammarProduction extends AbstractProduction {

	protected Production namespaceDeclarationProduction = null;

	protected Production includeDeclarationProduction = null;

	protected Production importDeclarationProduction = null;

	protected Production grammarDeclarationProduction = null;

	protected Production grammarBodyProduction = null;

	protected Coordinator coordinator = null;

	public void parse() throws Exception {
		tk = lexer.nexttok(false);
		if (tk.equalsIgnoreCase(KW_NAMESPACE)) {
			lexer.backward(tk);
			this.namespaceDeclarationProduction.setLexer(lexer);
			this.namespaceDeclarationProduction.parse();
			tk = lexer.nexttok(false);
		}
		if (tk.equals(KW_INCLUDE)) {
			lexer.backward(tk);
			this.includeDeclarationProduction.setLexer(lexer);
			do {
				this.includeDeclarationProduction.parse();
				tk = lexer.nexttok(false);
			} while (tk.equals(KW_INCLUDE));
		}
		if (tk.equals(JAVA_IMPORT)) {
			lexer.backward(tk);
			this.importDeclarationProduction.setLexer(lexer);
			do {
				this.importDeclarationProduction.parse();
				tk = lexer.nexttok(false);
			} while (tk.equals(JAVA_IMPORT));
		}
		if (tk.equalsIgnoreCase(KW_TEMPLATED)) {
			// dont backward the lexer here!
			if (coordinator.isPrimaryGrammar()) {
				lexer.error("templated grammar cannot be instantiated alone.");
			}
			tk = lexer.nexttok(KW_GRAMMAR);
		}
		if (tk.equalsIgnoreCase(KW_GRAMMAR)) {
			lexer.backward(tk);
			grammarDeclarationProduction.setLexer(lexer);
			grammarDeclarationProduction.parse();
		} else {
			grammarBodyProduction.setLexer(lexer);
			grammarBodyProduction.parse();
		}
	}

	public void setCoordinator(Coordinator coordinator) {
		this.coordinator = coordinator;
	}

	public void setGrammarBodyProduction(Production grammarBodyProduction) {
		this.grammarBodyProduction = grammarBodyProduction;
	}

	public void setGrammarDeclarationProduction(
			Production grammarDeclarationProduction) throws Exception {
		this.grammarDeclarationProduction = grammarDeclarationProduction;
	}

	/**
	 * @param importDeclarationProduction
	 *            the importDeclarationProduction to set
	 */
	public void setImportDeclarationProduction(
			Production importDeclarationProduction) {
		this.importDeclarationProduction = importDeclarationProduction;
	}

	public void setIncludeDeclarationProduction(
			Production includeDeclarationProduction) throws Exception {
		this.includeDeclarationProduction = includeDeclarationProduction;
	}

	public void setNamespaceDeclarationProduction(
			Production namespaceDeclarationProduction) throws Exception {
		this.namespaceDeclarationProduction = namespaceDeclarationProduction;
	}

}
